package ice.webapp.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActionFactory {

    /**
     * Action列表
     */
    private List<BaseAction> actionList = new ArrayList<BaseAction>();

    /**
     * 请求列表
     */
    private Map<String, BaseAction> requestMap = new HashMap<String, BaseAction>();

    /**
     * 单例
     */
    private static ActionFactory factory;

    /**
     * hide me
     */
    private ActionFactory() {
        initActionFactory();
    }

    /**
     * 获取工厂实例
     */
    public static ActionFactory getFactory() {
        if (factory == null) {
            factory = new ActionFactory();
        }
        return factory;
    }

    /**
     * 初始化
     */
    private void initActionFactory() {
        actionList.add(new TestAction());
        initRequestActionForAction();
    }

    /**
     * 初始化请求与Action的映射列表
     */
    private void initRequestActionForAction() {
        for (BaseAction action : actionList) {
            for (String requestUrl : action.getRequestUrlMap().keySet()) {
                requestMap.put(requestUrl, action);
            }
        }
    }

    /**
     * 根据请求Uri获取Action
     */
    public BaseAction getAction(String requestUri) {
        return requestMap.get(requestUri);
    }
}
